package cn.edu.scut.hsrc.bean;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import cn.edu.scut.hsrc.util.StringUtil;

/**
 * 文档类，搜索结果一条记录就是一个文档
 * @file Doc.java
 * @author feiyu
 *
 */

public class Doc {

	private final int index;				//文档编号
	private final String title;				//标题
	private final String summary;			//摘要
	private List<Segment> trunkedTitle;		//标题段落（只保留和查询词有强关联的段）
	private List<Segment> trunkedSummaray;	//摘要段落（只保留和查询词有强关联的段）
	
	private String site;					//来源站点
	private long time;						//结果时间
	/**
	 * 
	 * 构造函数
	 * @param index		文档编号
	 * @param title		标题
	 * @param summary	摘要
	 */
	public Doc(int index,String title,String summary) {
		// TODO Auto-generated constructor stub
		this.index = index;
		this.title = StringUtil.replaceWhiteSpace(title, ",");
		this.summary = StringUtil.replaceWhiteSpace(summary, ",");
		this.trunkedTitle = new ArrayList<Segment>();
		this.trunkedSummaray = new ArrayList<Segment>();
		this.site = "";
		this.time = -1;
	}
	
	/**
	 * 
	 * 构造函数
	 * @param index		文档编号
	 * @param title		标题
	 * @param summary	摘要
	 * @param site		来源站点
	 * @param time	          检索结果时间
	 */
	public Doc(int index,String title,String summary,String site,long time) {
		// TODO Auto-generated constructor stub
		this.index = index;
		this.title = StringUtil.replaceWhiteSpace(title, ",");
		this.summary = StringUtil.replaceWhiteSpace(summary, ",");
		this.trunkedTitle = new ArrayList<Segment>();
		this.trunkedSummaray = new ArrayList<Segment>();
		this.site = site;
		this.time = time;
	}
	
	
	
	/**
	 * 复制构造函数
	 * 构造函数
	 * @param doc
	 */
	public Doc(Doc doc) {
		// TODO Auto-generated constructor stub
		this.index = doc.index;
		this.title = doc.title;
		this.summary = doc.summary;
		this.trunkedTitle = doc.trunkedTitle;
		this.trunkedSummaray = doc.trunkedSummaray;
		this.site = doc.site;
		this.time = doc.time;
	}
	
	/**
	 * 
	 * @return 返回文档编号
	 */
	public int getIndex() {
		return this.index;
	}
	
	/**
	 * 
	 * @return  返回文档标题
	 */
	public String getTitle() {
		return this.title;
	}
	
	/**
	 * 
	 * @return 返回文档摘要
	 */
	public String getSummary() {
		return this.summary;
	}
	
	/**
	 * 
	 * @return 返回预处理后的文档标题
	 */
	public List<Segment> getTrunkedTitle() {
		return this.trunkedTitle;
	}
	
	/**
	 * 
	 * @return 设置预处理后的文档标题
	 */
	public void setTrunkedTitle(List<Segment> trunkedTitle) {
		this.trunkedTitle = trunkedTitle;
	}
	
	/**
	 * 
	 * @return 返回预处理后的文档摘要
	 */
	public List<Segment> getTrunkedSummaray() {
		return this.trunkedSummaray;
	}
	
	/**
	 * 
	 * @return 设置预处理后的文档摘要
	 */
	public void setTrunkedSummaray(List<Segment> trunkedSummaray) {
		this.trunkedSummaray = trunkedSummaray;
	}
	
	
	/**
	 * 获取来源站点
	 * @return
	 */
	public String getSite() {
		return site;
	}

	/**
	 * 设置来源站点
	 * @param site
	 */
	public void setSite(String site) {
		this.site = site;
	}

	/**
	 * 获取时间
	 * @return
	 */
	public long getTime() {
		return time;
	}

	/**
	 * 设置时间
	 * @param time
	 */
	public void setTime(long time) {
		this.time = time;
	}

	/**
	 * 判断两个文档是否相等，这里只是判断文档编号是否相等
	 */
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		if(obj instanceof Doc)
		{
			return this.index == ((Doc)obj).index;
		}
		return false;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		String text = "index: "+this.index+"\n";
		text += "title: "+this.title+"\n";
		text += "summary: "+this.summary+"\n";
		text += "trunkedTitle: ";
		for (Segment segment : this.trunkedTitle) {
			text += segment.getText()+",";
		}
		text = text.substring(0, text.length()-1);
		text += "\ntrunkedSummary: ";
		for (Segment segment : this.trunkedSummaray) {
			text += segment.getText()+",";
		}
		text = text.substring(0, text.length()-1);
		text += "\n";
		text += "site: "+site+"\n";
		if(this.time < 0)
			text += "time: 未知\n";
		else {
			Date date = new Date(this.time);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			text += "time: "+format.format(date)+"\n";
		}
		return text;
	}
}

